package org.example.stockdemo.job;

import lombok.extern.slf4j.Slf4j;
import org.example.stockdemo.entity.StockIndustryRank;
import org.example.stockdemo.entity.StockRank;
import org.example.stockdemo.service.StockIndustryRankService;
import org.example.stockdemo.service.StockRankService;
import org.example.stockdemo.service.TencentStockService;
import org.example.stockdemo.util.CacheUtil;
import org.example.stockdemo.util.StockUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :Mr.zhou
 * @description: 定时刷新股票数据任务
 * @create date: 2025/10/24 22:43
 */
@Slf4j
@Service
public class RankRealFlushJob {

    @Autowired
    private TencentStockService tencentStockService;
    @Autowired
    private StockRankService stockRankService;
    @Autowired
    private StockIndustryRankService industryRankService;


    @Scheduled(cron = "0/40 * 9-11 * * ?")
    @Scheduled(cron = "0 * 13-23 * * ?")
    public void netMainInRank() {
        try {
            if (CacheUtil.isFlush && (StockUtil.atAMRuntime() || StockUtil.atPMRuntime())) {
                String sortType = "netMainIn";  //资金净流入涨幅排行 netMainIn-down
                System.out.println("--------------资金净流入涨幅排行-----------------");
                List<StockRank> stockRankList = tencentStockService.moneyRank(sortType, "down", 10);
                // 保存
                stockRankService.batchSave(stockRankList);
                // 打印
                printStockRank(stockRankList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取排行数据失败:{}", e.getMessage());
        }
    }

    private void printStockRank(List<StockRank> stockRankList) {
        for (StockRank stockDetail : stockRankList) {
            System.out.println(stockDetail.getName() + "(" + stockDetail.getCode() + ") " + stockDetail.getPrice() + " " +
                    stockDetail.getChangePercent() + " " + stockDetail.getMainNetIn());
        }
    }

    /**
     * 涨幅榜
     */
    @Scheduled(cron = "0 0/2 9-11 * * ?")
    @Scheduled(cron = "0 0/5 13-23 * * ?")
    public void priceRatioRank() {
        try {
            if (CacheUtil.isFlush && (StockUtil.atAMRuntime() || StockUtil.atPMRuntime())) {
                //sortType=priceRatio  down 涨幅榜
                String sortType = "priceRatio";
                String direct = "down";
                List<StockIndustryRank> industryRankList = tencentStockService.industryRank(sortType, direct, 10);
                // 批量保存
                industryRankService.batchSave(industryRankList);
                System.out.println("--------------板块涨幅榜排行-----------------");
                printIndustryRank(industryRankList);


                //sortType=priceRatio up 跌幅榜
                direct = "up";
                industryRankList = tencentStockService.industryRank(sortType, direct, 10);
                industryRankService.batchSave(industryRankList);
                System.out.println("--------------板块跌幅排行-----------------");
                printIndustryRank(industryRankList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取排行数据失败:{}", e.getMessage());
        }
    }

    private void printIndustryRank(List<StockIndustryRank> stockRankList) {
        for (StockIndustryRank stockDetail : stockRankList) {
            System.out.println(stockDetail.getName() + "(" + stockDetail.getChangePercent() + ") -" + stockDetail.getTopStockName() + "【" + stockDetail.getTopStockCode() + "】-" +
                    stockDetail.getTopStockPrice() + " " + stockDetail.getTopStockChangePercent());
        }
    }

}